from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from typing import List
from fastapi import APIRouter, Depends, HTTPException
from ..core.database import get_db
from ..core.dependencies import get_current_admin_user, get_current_active_user
from ..models.user import User
from ..schemas.task import Task, TaskCreate, TaskUpdate
from ..services.task_service import TaskService

router = APIRouter(prefix="/tasks", tags=["tasks"])

@router.get("/", response_model=List[Task])
def get_tasks(skip: int = 0, limit: int = 100, db: Session = Depends(get_db), current_user: User = Depends(get_current_admin_user)):
    # 只有管理员可以查看任务列表
    task_service = TaskService(db)
    return task_service.get_tasks(skip, limit)

@router.post("/", response_model=Task)
def create_task(task: TaskCreate, db: Session = Depends(get_db), current_user: User = Depends(get_current_admin_user)):
    # 只有管理员可以创建任务
    task_service = TaskService(db)
    return task_service.create_task(task)

@router.get("/{task_id}", response_model=Task)
def get_task(task_id: int, db: Session = Depends(get_db), current_user: User = Depends(get_current_admin_user)):
    # 只有管理员可以查看任务详情
    task_service = TaskService(db)
    task = task_service.get_task(task_id)
    if not task:
        raise HTTPException(status_code=404, detail="Task not found")
    return task

@router.put("/{task_id}", response_model=Task)
def update_task(task_id: int, task: TaskUpdate, db: Session = Depends(get_db), current_user: User = Depends(get_current_admin_user)):
    # 只有管理员可以更新任务信息
    task_service = TaskService(db)
    return task_service.update_task(task_id, task)

@router.delete("/{task_id}")
def delete_task(task_id: int, db: Session = Depends(get_db), current_user: User = Depends(get_current_admin_user)):
    # 只有管理员可以删除任务
    task_service = TaskService(db)
    task_service.delete_task(task_id)
    return {"message": "Task deleted successfully"}

@router.get("/project/{project_id}", response_model=List[Task])
def get_tasks_by_project(project_id: int, db: Session = Depends(get_db), current_user: User = Depends(get_current_admin_user)):
    # 只有管理员可以查看指定项目的任务列表
    task_service = TaskService(db)
    return task_service.get_tasks_by_project(project_id)

@router.get("/user/me", response_model=List[Task])
def get_my_tasks(db: Session = Depends(get_db), current_user: User = Depends(get_current_active_user)):
    # 普通用户可以查看自己的任务列表
    task_service = TaskService(db)
    return task_service.get_tasks_by_user(current_user.id)

@router.get("/overdue", response_model=List[Task])
def get_overdue_tasks(db: Session = Depends(get_db), current_user: User = Depends(get_current_admin_user)):
    # 只有管理员可以查看逾期任务
    task_service = TaskService(db)
    return task_service.get_overdue_tasks()

@router.get("/stats")
def get_task_stats(db: Session = Depends(get_db), current_user: User = Depends(get_current_admin_user)):
    # 只有管理员可以查看任务统计信息
    task_service = TaskService(db)
    return task_service.get_task_stats_by_status()

@router.get("/stats/project/{project_id}")
def get_project_task_stats(project_id: int, db: Session = Depends(get_db), current_user: User = Depends(get_current_admin_user)):
    # 只有管理员可以查看指定项目的任务统计信息
    task_service = TaskService(db)
    return task_service.get_task_stats_by_status(project_id)